<?php

class Application_Form_Shop extends Zend_Form
{
    private $province = array(
        "Aceh","Bali","Banten","Bengkulu","D.I. Yogyakarta","D.K.I. Jakarta","Gorontalo","Jambi",
        "Jawa Barat","Jawa Tengah","Jawa Timur","Kalimantan Barat","Kalimantan Selatan",
        "Kalimantan Tengah","Kalimantan Timur","Kepulauan Bangka Belitung","Kepulauan Riau",
        "Lampung","Maluku","Maluku Utara","Nusa Tenggara Barat","Nusa Tenggara Timur","Papua",
        "Papua Barat","Riau","Sulawesi Barat","Sulawesi Selatan","Sulawesi Tengah","Sulawesi Tenggara",
        "Sulawesi Utara","Sumatera Barat","Sumatera Selatan","Sumatera Utara"
    );

    public function init()
    {
        //Hidden text field to pass id value
        //ID must be integer
        $id = new Zend_Form_Element_Hidden("id");
        $id->addFilter("Int");

        //Shop's name
        $name = new Zend_Form_Element_Text("name");
        $name->setLabel("Nama Toko")
                ->setRequired("true")
                ->addFilter("StripTags")
                ->addFilter("StringTrim")
                ->addValidator("NotEmpty")
                ->setAttrib("maxLength", "25")
                ->addErrorMessages(array("NotEmpty" => "Mohon isi nama toko"));

        //Shop's owner
        $owner = new Zend_Form_Element_Text("owner");
        $owner->setLabel("Nama Pemilik")
                ->setRequired()
                ->addFilter("StripTags")
                ->addFilter("StringTrim")
                ->addValidator("NotEmpty")
                ->setAttrib("maxLength", "30")
                ->addErrorMessages(array("NotEmpty" => "Mohon isi nama pemilik"));

        //Shop's address
        $address = new Zend_Form_Element_TextArea("address");
        $address->setLabel("Alamat")
             ->addFilter("StripTags")
             ->addFilter("StringTrim")
             ->setAttrib("cols","30")
             ->setAttrib("rows","4")
             ->setRequired()
             ->addValidator("NotEmpty")
             ->addErrorMessages(array("NotEmpty" => "Mohon isi alamat toko"));

        //Shop's city
        $city = new Zend_Form_Element_Text("city");
        $city->setLabel("Kota")
             ->setRequired("true")
             ->addFilter("StripTags")
             ->addFilter("StringTrim")
             ->addValidator("NotEmpty")
             ->setAttrib("maxLength","15")
             ->addErrorMessages(array("NotEmpty"=>"Mohon isi kota"));

        //Shop's province
        $provinces = array();
        foreach($this->province as $val){
            $provinces[$val] = $val;
        }
        $provinces["Lainnya"] = "Lainnya";
        $province = new Zend_Form_Element_Select("province");
        $province->setLabel("Provinsi")
             ->setRequired()
             ->addMultiOptions($provinces);

        //Shop's zip code
        $zip = new Zend_Form_Element_Text("zip");
        $zip->setLabel("Kode pos")
             ->setRequired("true")
             ->addFilter("StripTags")
             ->addFilter("StringTrim")
             ->addValidator("NotEmpty")
             ->addValidator("Digits")
             ->setAttrib("maxLength","5")
             ->addErrorMessages(array("NotEmpty"=>"Mohon isi kode pos dengan benar"));

        //Shop's phone number
        $phone = new Zend_Form_Element_Text("phone");
        $phone->setLabel("Nomor Telp. (dengan kode area)")
             ->setRequired("true")
             ->addFilter("StripTags")
             ->addFilter("StringTrim")
             ->addValidator("NotEmpty")
             ->addValidator("regex",true,array('pattern' => "/^0([0-9]{1,3}-)?[0-9]{6,}$/"))
             ->setAttrib("maxLength","15")
             ->addErrorMessages(array("regex"=>"Mohon isilah format nomor telepon dengan benar"));

        //Shop's email
        $email = new Zend_Form_Element_Text("email");
        $email->setLabel("Email")
             ->setRequired("true")
             ->addFilter("StripTags")
             ->addFilter("StringTrim")
             ->addValidator("EmailAddress")
             ->setAttrib("maxLength","100")
             ->addErrorMessages(array("EmailAddress"=>"Mohon isilah format email dengan benar"));

        //Shop's website
        $url = new Zend_Form_Element_Text("url");
        $url->setLabel("Website")
             ->setRequired()
             ->addFilter("StripTags")
             ->addFilter("StringTrim")
             ->addValidator("NotEmpty")
             ->setAttrib("maxLength","50")
             ->addErrorMessages(array("NotEmpty"=>"Mohon isi website toko"));

        //Shop's description/details
        $description = new Zend_Form_Element_TextArea("description");
        $description->setLabel("Tentang toko")
             ->addFilter("StripTags")
             ->addFilter("StringTrim")
             ->setAttrib("cols","30")
             ->setAttrib("rows","5");

        //Item sold in the shop
        $item_sold = new Zend_Form_Element_Text("item_sold");
        $item_sold->setLabel("Barang yang dijual")
             ->setRequired()
             ->addFilter("StripTags")
             ->addFilter("StringTrim")
             ->addValidator("NotEmpty")
             ->setAttrib("maxLength","50")
             ->addErrorMessages(array("NotEmpty"=>"Mohon isi daftar barang yang dijual"));

        //Shop's category
        //First, retrive all existing category from DB
        $cats_db = new Application_Model_DbTable_Category();
        $cats = $cats_db->fetchAll();
        $catsArr = array();
        foreach($cats as $cat){
            $catsArr[$cat->category_id] = $cat->name;
        }

        //Next, insert categories into select element
        $category = new Zend_Form_Element_Multiselect("category");
        $category->setRequired()
                 ->setLabel("Kategori (min. 1)")
                 ->addMultiOptions($catsArr);


        //Shop's membership
        //First, retrive all existing membership from DB
        $members_db = new Application_Model_DbTable_Membership();
        $members = $members_db->fetchAll();
        $membersArr = array();
        foreach($members as $member){
            $membersArr[$member->membership_id] = $member->name;
        }

        //Next, insert memberships into select element
        $membership = new Zend_Form_Element_Select("membership");
        $membership->setRequired()
                 ->setLabel("Jenis keanggotaan")
                 ->addMultiOptions($membersArr);

        //Item sold in the shop
        $keyword = new Zend_Form_Element_Text("keyword");
        $keyword->setLabel("Keyword/tag (pisahkan dengan ',')")
             ->addFilter("StripTags")
             ->addFilter("StringTrim")
             ->setAttrib("maxLength","50");
        
        //Picure to display
        $path = realpath('.')."\\images\\shops\\";
        $picture = new Zend_Form_Element_File('picture');
        $picture ->setLabel("Gambar")
                 ->setDestination($path)
                 ->addValidator('Count', false, 1)
                 ->addValidator('Size', false, 1024000)
                 ->addValidator('Extension', false, 'jpg,png,gif')
                 ->addErrorMessage(array("error"
                        =>"Mohon upload avatar dengan extension
                            .jpg,.png atau .gif"));

        //Submit button
        $submit = new Zend_Form_Element_Submit("submit");
        $submit->setAttrib("id","submit");

        //Reset button
        $reset = new Zend_Form_Element_Reset("reset");
        $reset->setAttrib("id","reset");

        //Add all elements to form
        $this->addElements(array($id,$name,$owner,$address,$city,$province,$zip,$phone,$email,$url,$description,
            $item_sold,$category,$keyword,$membership,$picture,$submit,$reset));
    }


}

